package git.soulbgm.common.exception;

import git.soulbgm.common.pojo.ResultData;
import git.soulbgm.common.pojo.ReturnCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * @author SoulBGM
 * @version 1.0
 * @date 2019/9/24 9:34
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 参数为空异常处理
     *
     * @param e
     * @return
     */
    @ExceptionHandler({MissingServletRequestParameterException.class, ParamIsNullException.class})
    public ResultData requestMissingServletRequest(Exception e) {
        log.error("request Exception:", e);
        return ResultData.getResultData(-1, e.getMessage());
    }

    /**
     * 特别说明： 可以配置指定的异常处理,这里处理所有
     *
     * @param request
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    public ResultData errorHandler(HttpServletRequest request, Exception e) {
        log.error("request Exception:", e);
        return ResultData.getResultData(-2, e.getMessage());
    }

}
